package com.hxzy.controller;

import com.hxzy.common.utils.ServletUtils;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.Menu;
import com.hxzy.service.MenuService;
import com.hxzy.vo.MenuTreeVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 后台首页 类说明
 *
 * @author admin
 * @date 2021-09-06
 */
@Controller
@RequestMapping(value = "/admin")
public class IndexController {

    @Autowired
    private MenuService menuService;

    /**
     * 后台首页
     * @return
     */
    @GetMapping(value = {"","/index"})
    public String index(Model model, HttpServletRequest request){
        AdminUser adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();
       //1、查询这个用户菜单有哪些
        List<MenuTreeVO> listMenu=this.menuService.findCurrentUserOwnerMenu(adminUser.getAdminUserId());

        model.addAttribute("menus",listMenu );

        model.addAttribute("user",adminUser);

        model.addAttribute("sideTheme",  "theme-dark");
        model.addAttribute("skinName", "skin-blue");
        model.addAttribute("ignoreFooter", "true");
        model.addAttribute("copyrightYear", "2021");
        model.addAttribute("demoEnabled", false);
        model.addAttribute("isDefaultModifyPwd",  false);
        model.addAttribute("isPasswordExpired", false);
        model.addAttribute("isMobile", ServletUtils.checkAgentIsMobile(request.getHeader("User-Agent")));

        // 菜单导航显示风格
        String menuStyle = "default";
        // 移动端，默认使左侧导航菜单，否则取默认配置
        String indexStyle = ServletUtils.checkAgentIsMobile(request.getHeader("User-Agent")) ? "index" : menuStyle;

        // 优先Cookie配置导航菜单
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies)
        {
            if (StringUtils.isNotEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName()))
            {
                indexStyle = cookie.getValue();
                break;
            }
        }

        //显示手机端，还是PC端
        String webIndex = "topnav".equalsIgnoreCase(indexStyle) ? "index-topnav" : "index";

        return webIndex;
    }

    @GetMapping(value = "/system/main")
    public String mainPage(Model model){

        return "main";
    }
}
